package net.entelijan.cobean.data.literature.persistence;

import java.util.List;

public class ClauseBuilder {

	private ClauseMapping mapping;

	private String separator;

	private String prefix;

	private ClauseBuilder() {
		super();
	}

	public ClauseBuilder(String prefix, String separator, ClauseMapping mapping) {
		this();
		this.prefix = prefix;
		this.mapping = mapping;
		this.separator = separator;
	}

	public ClauseBuilder(String separator, ClauseMapping mapping) {
		this(null, separator, mapping);
	}

	public ClauseBuilder(ClauseMapping mapping) {
		this(null, ",", mapping);
	}

	public String buildClause() {
		StringBuilder sb = new StringBuilder();
		List<?> clauseItems = mapping.getClauseItems();
		if (clauseItems != null && !clauseItems.isEmpty()) {
			if (prefix != null && !"".equals(prefix)) {
				sb.append(this.prefix);
				sb.append(" ");
			}
			for (int i = 0; i < clauseItems.size(); i++) {
				if (i > 0) {
					sb.append(this.separator);
					sb.append(" ");
				}
				sb.append(clauseItems.get(i).toString());
			}
		}
		return sb.toString();
	}
}
